package 蓝桥杯.算法板子.图论;

import java.io.*;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;

/**
 * @Title: Dijkstra
 * @Author 贾金博
 * @Package 蓝桥杯.算法板子.图论
 * @Date 2024/4/12 21:29
 * @description:
 */
public class Dijkstra {
    public static MyFastRead sc = new MyFastRead();
    public static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    public static int[] h, ne, e, w, dis;
    public static boolean[] st;
    public static int idx;
    public static Queue<Pair> q = new PriorityQueue<>();

    public static void main(String[] args) {
        int n = sc.nextInt();
        int m = sc.nextInt();
        h = new int[n + 10];
        ne = new int[m + 10];
        e = new int[m + 10];
        w = new int[m + 10];
        dis = new int[n + 10];
        st = new boolean[n + 10];
        init();
        for(int i = 1; i <= m; ++ i ) {
            int a, b, c;
            a = sc.nextInt();
            b = sc.nextInt();
            c = sc.nextInt();
            add(a, b, c);
        }
        q.add(new Pair(0, 1));
        st[1] = true;

        while(!q.isEmpty()) {
            Pair t = q.poll();
            int pos = t.y; // 得到这个点 它是距离原点较近的
            st[pos] = false; // 它被删除了
            for(int i = h[pos]; i != -1; i = ne[i]) {
                int j = e[i];
                if(dis[j] > dis[pos] + w[i]) {
                    dis[j] = dis[pos] + w[i];
                    if(!st[j]) {
                        st[j] = true;
                        q.add(new Pair(dis[j], j));
                    }
                }
            }
        }
        if(dis[n] == 0x3f3f3f3f) {
            out.println(-1);
        } else {
            out.println(dis[n]);
        }
        out.flush();
    }

    private static void init() {
        Arrays.fill(h, -1);
        Arrays.fill(dis, 0x3f3f3f3f);
        idx = 0;
        dis[1] = 0;
    }

    public static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx ++ ;
    }

}

class Pair implements Comparable<Pair> {

    int x;
    int y;

    public Pair(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public int compareTo(Pair o) {
        return this.x - o.x;
    }
}

 class MyFastRead {
    //快读的模板
    StringTokenizer st;
    BufferedReader br;
    public MyFastRead(){
        br=new BufferedReader(new InputStreamReader(System.in));
    }

    String next(){
        while (st==null||!st.hasMoreElements()){
            try {
                st=new StringTokenizer(br.readLine());
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }
    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }
    double nextDouble() {
        return Double.parseDouble(next());
    }
    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}


